
package com.yimixian.app.cart;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.yimixian.app.R;
import com.yimixian.app.activity.BaseNaviFragmentGroupActivity;
import com.yimixian.app.activity.YMXMainActivity;
import com.yimixian.app.common.Constants;
import com.yimixian.app.goods.GoodDetailTopFragment;
import com.yimixian.app.util.SharedPreferencesHelper;
import com.yimixian.app.util.UiUtils;

import java.util.ArrayList;

/**
 * Created by szy on 2016/1/18.
 */
public class HuanGouActivity extends BaseNaviFragmentGroupActivity implements View.OnClickListener {
    private EditText mSearchView;
    private LinearLayout mSearchViewContainer;
    private RelativeLayout mSearchButton;
    private RelativeLayout mClearSearchButton;
    public boolean mIsStartSearch = false;
    public RelativeLayout mAnimattionContainer, keyboard_layout;

    @Override
    protected void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        View view = View.inflate(this,R.layout.activity_navibar_simple,null);
        RelativeLayout titleBar = (RelativeLayout) view.findViewById(R.id.title_bar);
//        titleBar.setBackgroundColor(getResources().getColor(R.color.search_bg));
        setContentView(view);
        mSearchViewContainer = (LinearLayout) view.findViewById(R.id.search_view_container);
        mSearchViewContainer.setVisibility(View.VISIBLE);
        mSearchViewContainer.setBackgroundColor(Color.TRANSPARENT);
        mAnimattionContainer = (RelativeLayout) view.findViewById(R.id.keyboard_layout);
        mSearchButton = (RelativeLayout) view.findViewById(R.id.rl_search);
        mSearchButton.setVisibility(View.INVISIBLE);
        mClearSearchButton = (RelativeLayout) view.findViewById(R.id.rl_clear_search);
        mClearSearchButton.setVisibility(View.INVISIBLE);
        mClearSearchButton.setOnClickListener(this);
        mSearchButton.setOnClickListener(this);
        setTopNavi(R.drawable.icon_arrow_back, "去商城凑单", "换  购", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                YMXMainActivity.needChooseKindTab = true;
                setResult(GoodDetailTopFragment.RETURN_TO_CATEGORY);
                finish();
            }
        }, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        TextView tv_title = (TextView) view.findViewById(R.id.tv_title);
        tv_title.setText("换  购");
        tv_title.setVisibility(View.VISIBLE);
//        TextView title_bar_right_text = (TextView) view.findViewById(R.id.title_bar_right_text);
//        title_bar_right_text.setText("去商城凑单");
//        title_bar_right_text.setVisibility(View.VISIBLE);
//        title_bar_right_text.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                YMXMainActivity.needChooseKindTab = true;
//                setResult(GoodDetailTopFragment.RETURN_TO_CATEGORY);
//                finish();
//            }
//        });
        mSearchView = (EditText) view.findViewById(R.id.edit_search);
        mSearchView.setVisibility(View.INVISIBLE);
//        mSearchView.addTextChangedListener(new TextWatcher() {
//            @Override
//            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
//            }
//
//            @Override
//            public void onTextChanged(CharSequence s, int start, int before, int count) {
//            }
//
//            @Override
//            public void afterTextChanged(Editable s) {
//                if (TextUtils.isEmpty(s) && mIsStartSearch) {
//                    if (mCurrentPrimaryFragment != null && mCurrentPrimaryFragment instanceof HuanGouFragment) {
//                        ((HuanGouFragment) mCurrentPrimaryFragment).showTips();
//                        mIsStartSearch = false;
//                    }
//                }
//            }
//        });
//        setTopNavi(R.drawable.icon_arrow_back,
//                "", "",
//                null, null);

        new Handler().post(new Runnable() {
            @Override
            public void run() {
                if(mCurrentPrimaryFragment != null){
                    startSearch("苹果");
                }
            }
        });

    }

    public void startSearch(String s) {
        ((HuanGouFragment) mCurrentPrimaryFragment).startSearch(s);
    }

    public void setSearchViewText(String s) {
//        if(mSearchView != null && !TextUtils.isEmpty(s)) {
//            mSearchView.setText(s);
//            mSearchView.setSelection(s.length());
//        }
    }

    public static Intent buildIntent(Context context) {
        Intent intent = new Intent(context, HuanGouActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
        return intent;
    }

    @Override
    protected void initPrimaryFragment() {
        switchPrimaryFragment(0);
    }

    @Override
    protected Class<? extends Fragment> getPrimaryFragmentClass(int fragmentId) {
        return HuanGouFragment.class;
    }

    @Override
    protected Bundle getPrimaryFragmentArguments(int fragmentId) {
        Bundle bundle = new Bundle();
        return bundle;
    }

    @Override
    protected int getPrimaryFragmentStubId(int fragmentId) {
        return R.id.content_frame;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.rl_search:
                clickSearch();
                break;
            case R.id.rl_clear_search:
                if (mSearchView != null) {
                    mSearchView.setText("");
                }
                mIsStartSearch = false;
                break;
        }
    }

    private void clickSearch() {
        if(UiUtils.isFastDoubleClick()) {
            return ;
        }
        if (mCurrentPrimaryFragment != null && mCurrentPrimaryFragment instanceof HuanGouFragment) {
            if(mSearchView != null && mSearchView.getText() != null) {
                String searchText = mSearchView.getText().toString();
                notifyHistoryList(searchText);
            }
        }
    }

    public void notifyHistoryList(String searchText) {
        if (!TextUtils.isEmpty(searchText) && !TextUtils.isEmpty(searchText.trim())) {
            ArrayList<String> histories = SharedPreferencesHelper.getSearchHistory();
            if(histories == null) {
                histories = new ArrayList<String>();
            }
            if(histories.contains(searchText.trim())) {
                histories.remove(searchText.trim());
            }
            histories.add(0, searchText.trim());
            if(histories.size() > 10) {
                histories.remove(10);
            }
            SharedPreferencesHelper.saveSearchHistory(histories);
            startSearch(searchText.trim());
        } else {
            if(mSearchView != null) {
                mSearchView.setText("");
            }
            mIsStartSearch = false;
        }
    }

    @Override
    public void onReiveServiceNotice(String action) {
        super.onReiveServiceNotice(action);
        if (Constants.ServicePageType.SEARCHFRAGMENT.equals(action)) {
            if (mCurrentPrimaryFragment != null)
                ((HuanGouFragment)mCurrentPrimaryFragment).isNeedUpdate();
        }
    }
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(resultCode == HuanGouFragment.FROM_COU_DAN){
            finish();
            return;
        }
        if (resultCode == RESULT_OK) {
            if(Constants.CART_REGIST_REQUEST_CODE == requestCode) {
                //从购物车跳转到登陆页面 登陆完成跳转到购物车页面
                startActivity(CartDetailActivity.buildIntent(this));
                overridePendingTransition(R.anim.slide_in_from_bottom, 0);
            }
        }else if(resultCode == GoodDetailTopFragment.RETURN_TO_CATEGORY){
            YMXMainActivity.needChooseKindTab = true;
            setResult(GoodDetailTopFragment.RETURN_TO_CATEGORY);
            finish();
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

    public Fragment getHuanGouFragment() {
        return mCurrentPrimaryFragment;
    }

    @Override
    protected void onDestroy() {
        clearServiceMapData(Constants.ServicePageType.SEARCHFRAGMENT);
        super.onDestroy();
    }

    @Override
    public void onBackPressed() {
        finish();
        overridePendingTransition(0, R.anim.activity_slide_right_out);
    }

    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        if(event.getKeyCode() == KeyEvent.KEYCODE_ENTER){
            /*隐藏软键盘*/
            clickSearch();
            UiUtils.closeSoftKeyBoard(this);
            return true;
        }
        return super.dispatchKeyEvent(event);
    }
}
